package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnBindMobil;
import com.lc.luzhengtang.conn.ConnVerificationCode;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/3.
 */

public class BindMobilActivity extends BaseActivity {


    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.mine_head_iv)
    SimpleDraweeView mineHeadIv;
    @BindView(R.id.text_username)
    TextView textUsername;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edittext_nickName)
    EditText edittextNickName;
    @BindView(R.id.img_phone)
    ImageView imgPhone;
    @BindView(R.id.img_sendcode)
    ImageView imgSendcode;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.layout_send_ben)
    LinearLayout layoutSendBen;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bindmobil);
        ButterKnife.bind(this);
        mineHeadIv.setImageURI(Uri.parse(getIntent().getStringExtra("pic_url")));
        textUsername.setText(getIntent().getStringExtra("name"));


    }

    @OnClick({R.id.img_news, R.id.mine_head_iv, R.id.text_username, R.id.btn_login, R.id.edittext_nickName, R.id.img_phone, R.id.img_sendcode, R.id.edit_code, R.id.layout_send_ben, R.id.text_sendbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.mine_head_iv:
                break;
            case R.id.text_username:
                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(BindMobilActivity.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 6 || editPhone.getText().toString().trim().length() > 16) {

                    LovelyToast.makeText(BindMobilActivity.this, "请输入正确的手机号码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(BindMobilActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                    LovelyToast.makeText(BindMobilActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editCode.getText().toString().equals(myCode)) {

                    LovelyToast.makeText(BindMobilActivity.this, "验证码不正确", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    bindmobil();
                }
                break;

            case R.id.edittext_nickName:
                break;
            case R.id.img_phone:
                break;
            case R.id.img_sendcode:
                break;
            case R.id.edit_code:
                break;
            case R.id.layout_send_ben:
                break;
            case R.id.text_sendbtn:

                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(BindMobilActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(BindMobilActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(BindMobilActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }


                break;

        }
    }


    String myCode;
    String code = "";

    public void getVerificationCode() {
        new ConnVerificationCode(editPhone.getText().toString().trim(), "reg", new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {
                super.onSuccess(toast, type, o, registerBean);
                if (registerBean.getCode() == 200) {
                    LovelyToast.makeText(BindMobilActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
//                btRegistered.setSelected(true);
                    countTime.start();
                    myCode = registerBean.getData().getCode() + "";
                }else if (registerBean.getCode()==400){
                    LovelyToast.makeText(BindMobilActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(false);
                textSendbtn.setText("获取验证码");
                countTime.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
//                countTime.start();
            }
        }


        ).execute(this);
    }


    public void bindmobil() {
        new ConnBindMobil(editPhone.getText().toString(), new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {

                }

                LovelyToast.makeText(BindMobilActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
               /*

                BaseApplication.basePreferences.saveOpenId(getIntent().getStringExtra("openid"));*/
                BaseApplication.basePreferences.saveUid(getIntent().getStringExtra("openid"));
                BaseApplication.basePreferences.saveUserName(getIntent().getStringExtra("name"));
                BaseApplication.basePreferences.saveAvatar(getIntent().getStringExtra("pic_url"));
                BaseApplication.basePreferences.saveBindFlag("1");
//                startActivity(new Intent(BindMobilActivity.this, MainActivity.class));
                Intent intent = new Intent();

                intent.putExtra("openid", getIntent().getStringExtra("openid"));
                intent.putExtra("pic_url", getIntent().getStringExtra("pic_url"));
                intent.putExtra("name", getIntent().getStringExtra("name"));
                intent.putExtra("mobil", editPhone.getText().toString());
                intent.setClass(BindMobilActivity.this, LoginNextActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }

        ).execute(BindMobilActivity.this, false);


    }


}
